/**
  *		Ankara University 
  * 	Computer Engineering Department
  *		Lab 5
  *		Subject : Computed Chain
  * 	Author : Haydar Kulekci
  *
  */

#include<stdio.h>
#include<stdlib.h>

#define DEFAULT_TABLE_SIZE 5
#define DEFAULT_CHAIN_NUMBER 2


int **createTable(int tableSize){
	int i,j;
	int **table;
	
	table = (int**) malloc( DEFAULT_CHAIN_NUMBER * sizeof(int) );
	for (i=0;i<DEFAULT_CHAIN_NUMBER;i++){
        table[0] = (int*) malloc( tableSize * sizeof(int) );
    	table[1] = (int*) malloc( tableSize * sizeof(int) );
     }
	
	
    for (i=0 ;i < tableSize ;i++ ){
        for (j=0;j<DEFAULT_CHAIN_NUMBER;j++){
            table[j][i] = -1;  
        }
	}
	
	return table;
	
}

int **switchArray(int **old, int tableSize){
	int i,j;
	int **newAr = createTable(tableSize);
    for (i=0 ;i < tableSize ;i++ ){
        for (j=0;j<DEFAULT_CHAIN_NUMBER;j++){
            newAr[j][i] = old[j][i];  
        }
	}
}

void print(int **table, int tableSize){
	int i,j;
	printf("\n");
	for (i=0 ;i < tableSize ;i++ ){
        for (j=0;j<DEFAULT_CHAIN_NUMBER;j++){
            
            printf("%d ",table[j][i]);  
        }
        printf("\n");
	}
	printf("\n");
}


int hash(int num,int tableSize){
	return num%tableSize;
}

int hashTo(int num,int tableSize){
	return (( (num/tableSize)%tableSize == 0 )?(1):((num/tableSize)%tableSize));
}

int getNewTableSize(int old, int now){
	int newTableSize, temp_pri, i, j;
	if (now == 0)
	   return old;
	if (old < 2){
		return 2;
	}
	for (i = old + 1 ;i < 1000 ;i++ ){
		temp_pri = 0;
		for (j=i-1; j>=2 ;j-- ){
			if (i%j == 0){
				temp_pri = 1;
			}
		}
		if (temp_pri == 0){
			return i;
		}
	}
	return 2;
}

int checkPackingFactor(int numOfElements,int tableSize){
	return ((numOfElements / tableSize * 100 < 95)?(1):(0)); 
}


// return num of elements
int loadFromFile(int **tk,int tableSize){
    /********************************/
	FILE *fp;
	int i = 0,sc,numOfElements = 0;
	fp = fopen("numbers.txt","r");
	do{
		fscanf(fp,"%5d",&sc);
		tk[i][i%DEFAULT_CHAIN_NUMBER] = sc;
		if (sc >= 0) numOfElements++;
		printf("%d\t",sc);
		if (i >= tableSize){
			break;
		}
		i++;
	}while(!feof(fp));
	printf("File is loaded!\n");
	return numOfElements;
}

void saveToFile(const int **tk, int tableSize){
     /********************************/
	FILE *fp;
	int i,j;
	
	fp = fopen("numbers.txt","w");

	for (i=0 ;i < tableSize ;i++ ){
        for (j=0;j<DEFAULT_CHAIN_NUMBER;j++){
            fprintf(fp,"%5d ",tk[j][i]);  
        }
        fprintf(fp,"\n");
	}
	fclose(fp);
}



void insert(int **table, int tableSize, int number){
     int active_index = hash(number,tableSize);
     
     if (table[0][active_index] == -1) { table[0][active_index] == number; return 1; }
     if (table[0][active_index] == number) { return 0; }
     if (hash(table[0][active_index],tableSize) != active_index){
          table[0][active_index] = number;
          // coalesced olanlar eklenecek.
     }
     while (table[1][active_index] <= -1)
}

int main(){
	int tableSize = DEFAULT_TABLE_SIZE;
	int **table;
	int numofnumner = 0; 
	
	table = createTable(tableSize);
	numofnumner = loadFromFile(table,tableSize);
	print(table,tableSize);
	
	getch();
	free(table);
	return 0;
}


